package com.lk.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lk.springboot.dao.SitesPhotoMapper;
import com.lk.springboot.service.SitesPhotoService;
import com.lk.springboot.vo.TheSiteSP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author lk
 * @date 2020/1/10 0010 19:01
 */
@Service
public class SitesPhotoServiceImpl implements SitesPhotoService {
    @Autowired
    SitesPhotoMapper sitesPhotoMapper;

    @Override
    public List getSPOfSites(String u_id, String s_id) {
        HashSet<Integer> allset = new HashSet<>();//所有标签（4个）
        HashSet<Integer> myset = new HashSet<>();//已经拥有的
        allset.add(1);
        allset.add(2);
        allset.add(3);
        allset.add(4);
        List<TheSiteSP> spList = sitesPhotoMapper.getAllSP(u_id, s_id);
        if (spList.size() < 4) {
            for (TheSiteSP theSiteSP : spList) {//获取用户已经获得的碎片的标志位
                myset.add(theSiteSP.getTags());
            }
            allset.removeAll(myset);//去掉用户已获得，得到用户为得到的彩色碎片的标志位
            for (Integer integer : allset) {//计算出未获得的对应的灰图的标志位，查询出未获得的碎片
                int t = integer.intValue() + 10;
                spList.add(sitesPhotoMapper.getMySP(t, s_id));
            }
        }
        List<JSONObject> list = new ArrayList<>();
        for (TheSiteSP theSiteSP : spList) {
            JSONObject returnJson = new JSONObject();
            returnJson.put("sp_id", theSiteSP.getSp_id());
            returnJson.put("url", theSiteSP.getUrl());
            if (theSiteSP.getTags() < 10) {
                returnJson.put("num", sitesPhotoMapper.getTheSmallPhoto(u_id, s_id, theSiteSP.getSp_id()));
                returnJson.put("tag", true);
            } else {
                returnJson.put("num", 0);
                returnJson.put("tag", false);
            }
            list.add(returnJson);
        }
        Collections.sort(list, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject o1, JSONObject o2) {
                return (o1.getString("sp_id").substring(3, 7)).compareTo((o2.getString("sp_id").substring(3, 7)));
            }
        });
        return list;
    }

}
